<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\api\model\CouponLog;
use think\exception\ValidateException;
use app\api\BaseController;
use app\api\validate\Coupon as Validate;
use app\api\model\Coupon as CouponModel;
/**
 * Coupon管理
 */
class Coupon extends BaseController
{
    /**
     * 显示资源列表
     * 访问：local.stop.com/api/coupon/index
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $input = input("post.");
            $page=input("post.page");
            $page_size=input("post.page_size");
            $page=$page?$page:1;
            $page_size=$page_size?$page_size:15;
            if(empty( $this->request->userId )){
                return json(['status'=>'login', 'message'=> '请登录后操作！']);
            }
            $count =(new CouponLog())->where('user_id', $this->request->userId)->count();
            $list =(new CouponLog())->where('user_id', $this->request->userId)->page($page, $page_size)->select()->toArray();
            $totalPages = ceil($count / $page_size);
            $data['list']=$list;
            $data['count']=$count;
            $data['totalPages']=$totalPages;
            return json(["status" => "success", "message" => "请求成功", "data" => $data]);
        }
    }
    
    /**
     * 显示资源详情
     */
    public function single()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("single")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            $data = CouponModel::find($input["id"]);
            return json(["status" => "success", "message" => "请求成功", "data" => $data]);
        }
    }
    
    /**
     * 保存新建的资源
     */
    public function save()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("save")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            CouponModel::create($input);
            return json(["status" => "success", "message" => "添加成功"]);
        }
    }
    
    /**
     * 保存更新的资源
     */
    public function update()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("update")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            CouponModel::update($input);
            return json(["status" => "success", "message" => "修改成功"]);
        }
    }
    
    /**
     * 删除指定资源
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("delete")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            CouponModel::destroy($input["ids"]);
            return json(["status" => "success", "message" => "删除成功"]);
        }
    }
    public function give(){
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                if(empty( $this->request->userId )){
                    return json(['status'=>'login', 'message'=> '请登录后操作！']);
                }
                validate(Validate::class)->scene("give")->check($input);
                $coupon=(new CouponLog())->where('qr_sign',$input['qr_sign'])->findOrEmpty()->toArray();
                if(empty($coupon)){
                    return json(["status" => "error", "message" => "兑换码不存在"]);
                }
                if($coupon['user_id']!=0){
                    return json(["status" => "error", "message" => "兑换码已被领取"]);
                }
                if(date('Y-m-d H:i:s',time()-3600*24)>$coupon['end_time']){
                    return json(["status" => "error", "message" => "兑换码已过期"]);
                }
                (new CouponLog())->where('qr_sign',$input['qr_sign'])->update(['user_id'=>$this->request->userId,'status'=>1]);
                return json(["status" => "success", "message" => "领取成功"]);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
        }
    }

}